#!/bin/bash

echo "启动新版价格策略分析平台 (使用ml虚拟环境)..."
echo "时间: $(date)"

# 设置虚拟环境路径
VENV_PYTHON="/opt/homebrew/Caskroom/miniforge/base/envs/ml/bin/python"

# 检查虚拟环境是否存在
if [ ! -f "$VENV_PYTHON" ]; then
    echo "错误: 虚拟环境不存在: $VENV_PYTHON"
    echo "请确保ml虚拟环境已创建"
    exit 1
fi

echo "使用虚拟环境: $VENV_PYTHON"

# 设置环境变量
export PYTHONPATH="${PYTHONPATH}:$(pwd)"

# 创建必要的目录
mkdir -p logs
mkdir -p data
mkdir -p cache

# 检查依赖
echo "检查依赖包..."
$VENV_PYTHON -c "import streamlit, pandas, numpy, matplotlib, seaborn, sqlalchemy, gurobipy, loguru" 2>/dev/null
if [ $? -ne 0 ]; then
    echo "错误: 依赖包未安装，请运行:"
    echo "$VENV_PYTHON -m pip install streamlit pandas numpy matplotlib seaborn sqlalchemy gurobipy loguru openpyxl scipy pyhive"
    exit 1
fi

echo "✅ 所有依赖包已安装"

# 启动应用
echo "启动Streamlit应用..."
echo "访问地址: http://localhost:8502"
echo "按 Ctrl+C 停止应用"

$VENV_PYTHON -m streamlit run new_app.py \
    --server.port 8502 \
    --server.address 0.0.0.0 \
    --server.headless true \
    --browser.gatherUsageStats false

echo "应用已停止"
echo "时间: $(date)"